<?php
	function getActivationFields($item)
	{
		if($item)
		{
			$output = '<img style="cursor:pointer;"';
			$output .= ' src="../images/system/active_'.$item['active'].'_25.gif"';
			if($item['active'] == 0)
			{ $output .= ' alt="activate" onmousedown="document.getElementById(\'activate\').value='.$item['id'].';"'; }
			else
			{ $output .= ' alt="deactivate" onmousedown="document.getElementById(\'deactivate\').value='.$item['id'].';"'; }
			$output .= ' onmouseup="edit_form.submit();" />';
			return $output;
		}
		
	}
	
	function getClients()
	{
		$i=0;
		$result = mysql_query('SELECT * FROM '.DB_PERFIX.'admin_clients ORDER BY id ASC;');
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['name'] = $row['name'];
			$item[$i]['contact'] = $row['contact'];
			$item[$i]['email'] = $row['email'];
			$item[$i]['msn'] = $row['msn'];
			$item[$i]['skype'] = $row['skype'];
			$item[$i]['description'] = $row['description'];
			$i++;
		}
		return $item;
	}
	
	function getSiteThemes($siteId=0)
	{
		$i = 0;
		$selection = 'SELECT * FROM '.DB_PERFIX.'themes WHERE site_id='.$siteId.';';
		$result = mysql_query($selection);
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id']					= $row['id'];
			$item[$i]['title'] 				= $row['title'];
			$item[$i]['site_id']			= $row['site_id'];
			$item[$i]['site_default'] 		= $row['site_default'];
			$item[$i]['theme_type']			= $row['theme_type'];
			$item[$i]['fixed_width']		= $row['fixed_width'];
			$item[$i]['horisontal_pos']		= $row['horisontal_pos'];
		
			$item[$i]['border_site']		= $row['border_site'];
			$item[$i]['border_content'] 	= $row['border_content'];
		
			$item[$i]['header']				= $row['header']; 
			$item[$i]['header_fixed']		= $row['header_fixed']; 
			$item[$i]['header_border'] 		= $row['header_border'];
			$item[$i]['header_hfb'] 		= $row['header_hfb'];
			$item[$i]['header_content'] 	= $row['header_content'];
			
			$item[$i]['footer']				= $row['footer'];
			$item[$i]['footer_fixed']		= $row['footer_fixed']; 
			$item[$i]['footer_border'] 		= $row['footer_border'];
			$item[$i]['footer_hfb'] 		= $row['footer_hfb'];
			$item[$i]['footer_content'] 	= $row['footer_content'];
			
			$item[$i]['col_left']			= $row['col_left'];
			$item[$i]['col_left_border'] 	= $row['col_left_border'];
			$item[$i]['col_left_hfb'] 		= $row['col_left_hfb'];
			$item[$i]['col_left_content'] 	= $row['col_left_content'];
			
			$item[$i]['col_right']			= $row['col_right'];
			$item[$i]['col_right_border'] 	= $row['col_right_border'];
			$item[$i]['col_right_hfb'] 		= $row['col_right_hfb'];
			$item[$i]['col_right_content'] 	= $row['col_right_content'];
			
			$item[$i]['hfb_set'] 			= $row['hfb_set'];
			$item[$i]['hfb_com_set'] 		= $row['hfb_com_set'];
			
			if(!file_exists(SITE_PATH.'templates/sites/'.$item[$i]['site_id'].'/layout'))
			{ mkdir(SITE_PATH.'templates/sites/'.$item[$i]['site_id'].'/layout'); }
			if(!file_exists(SITE_PATH.'templates/themes/'.$item[$i]['id']))
			{ mkdir(SITE_PATH.'templates/themes/'.$item[$i]['id']); }
			if(!file_exists(SITE_PATH.'templates/themes/'.$item[$i]['id'].'/images'))
			{ mkdir(SITE_PATH.'templates/themes/'.$item[$i]['id'].'/images'); }
			$i++;
			
		}
		return $item;
	}

	function getSitesList($clientId=0)
	{
		$i=0;
		$select = 'id, client_id, title, site_url, license, language, meta_data, meta_key, analytics_id, active';
		$where = '';
		if($clientId > 0)
		{
			$where = 'WHERE client_id='.$clientId;
		}
		$result = mysql_query('SELECT '.$select.' FROM '.DB_PERFIX.'sites '.$where.' ORDER BY id ASC;');
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] 			= $row['id'];
			$item[$i]['client_id'] 		= $row['client_id'];
			$item[$i]['title'] 			= $row['title'];
			$item[$i]['site_url'] 		= $row['site_url'];
			$item[$i]['license'] 		= $row['license'];
			$item[$i]['language']		= $row['language'];
			$item[$i]['meta_data'] 		= $row['meta_data'];
			$item[$i]['meta_key'] 		= $row['meta_key'];
			$item[$i]['analytics_id'] 	= $row['analytics_id'];
			$item[$i]['active'] 		= $row['active'];
			if(!file_exists(SITE_PATH.'templates/sites/'.$item[$i]['id']))
			{ mkdir(SITE_PATH.'templates/sites/'.$item[$i]['id']); }
			if(!file_exists(SITE_PATH.'templates/sites/'.$item[$i]['id'].'/themes'))
			{ mkdir(SITE_PATH.'templates/sites/'.$item[$i]['id'].'/themes'); }
			$i++;
		}
		return $item;
	}
	
	function getThemeTypes()
	{
		$i=0;
		$result = mysql_query("SELECT id, type, title, header, footer, col_left, col_right FROM ".DB_PERFIX."admin_css_theme_types ORDER BY type ASC;");
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['type'] = $row['type'];
			$item[$i]['title'] = $row['title'];
			$item[$i]['header'] = $row['header'];
			$item[$i]['footer'] = $row['footer'];
			$item[$i]['col_left'] = $row['col_left'];
			$item[$i]['col_right'] = $row['col_right'];
			$i++;
		}
		return $item;
	}
	
	function getThemeType($id)
	{
		if($id > 0)
		{
			$result = mysql_query("SELECT id, type, title, header, footer, col_left, col_right FROM ".DB_PERFIX."admin_css_theme_types WHERE id=".$id." ORDER BY type ASC;");
			while($row = mysql_fetch_array($result))
			{
				$item['id'] = $row['id'];
				$item['type'] = $row['type'];
				$item['title'] = $row['title'];
				$item['header'] = $row['header'];
				$item['footer'] = $row['footer'];
				$item['col_left'] = $row['col_left'];
				$item['col_right'] = $row['col_right'];
			}
			return $item;
		}
	}
	
	function getIconSet($id)
	{
		$result = mysql_query('SELECT * FROM '.DB_PERFIX.'icon_sets WHERE id='.id.';');
		while($row = mysql_fetch_array($result))
		{
			$item['id'] = $row['id'];
			$item['name'] = $row['name'];
			$item['width'] = $row['width'];
			$item['height'] = $row['height'];
		}
		return $item;
	}
	function getIconSetList()
	{
		$i=0;
		$result = mysql_query("SELECT * FROM ".DB_PERFIX."icon_sets;");
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['name'] = $row['name'];
			$item[$i]['width'] = $row['width'];
			$item[$i]['height'] = $row['height'];
			$i++;
		}
		return $item;
	}
	
	function getBordersList()
	{
		$i=0;
		$result = mysql_query("SELECT id, name FROM ".DB_PERFIX."com_borders;");
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['name'] = $row['name'];
			$i++;
		}
		return $item;
	}
	
	function getHeadersList()
	{
		$i=0;
		$result = mysql_query("SELECT * FROM ".DB_PERFIX."com_headers;");
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['name'] = $row['name'];
			$i++;
		}
		return $item;
	}
	
	function getFootersList()
	{
		$i=0;
		$result = mysql_query("SELECT * FROM ".DB_PERFIX."com_footers;");
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['name'] = $row['name'];
			$i++;
		}
		return $item;
	}
	
	function getButtonsList()
	{
		$i=0;
		$result = mysql_query("SELECT * FROM ".DB_PERFIX."buttons;");
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['name'] = $row['name'];
			$i++;
		}
		return $item;
	}
	
	function getMenuTypesList()
	{
		$i=0;
		$result = mysql_query("SELECT * FROM ".DB_PERFIX."com_menu;");
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['name'] = $row['name'];
			$i++;
		}
		return $item;
	}
	
	function getHFBSetList()
	{
		$i=0;
		$result = mysql_query("SELECT id, name FROM ".DB_PERFIX."hfb_sets;");
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['name'] = $row['name'];
			$i++;
		}
		return $item;
	}
	
	function getHFBCats()
	{
		$i=0;
		$result = mysql_query("SELECT * FROM ".DB_PERFIX."admin_hfb_cats;");
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['title'] = $row['title'];
			$i++;
		}
		return $item;
	}
	
	function getHFBCatItems($catid, $type)
	{
		$i=0;
		$select = ' SELECT item_id FROM '.DB_PERFIX.'admin_hfb_cats_to_items ';
		$select = $select.' WHERE type='.$type.' AND cat_id='.$catid.';';
		$result = mysql_query($select);
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['item_id'] = $row['item_id'];
			$i++;
		}
		return $item;
	}
	
	function getInHFBCats($itemid, $type)
	{
		$i=0;
		$select = ' SELECT cti.id, cti.cat_id, c.title FROM '.DB_PERFIX.'admin_hfb_cats AS c ';
		$select = $select.' LEFT JOIN '.DB_PERFIX.'admin_hfb_cats_to_items AS cti ON cti.cat_id=c.id ';
		$select = $select.' WHERE cti.item_id='.$itemid.' AND cti.type='.$type.' ORDER BY c.title ASC;';
		
		$result = mysql_query($select);
		if($result)
		{
			while($row = mysql_fetch_array($result))
			{
				$item[$i]['id'] = $row['cat_id'];
				$item[$i]['title'] = $row['title'];
				$i++;
			}
			return $item;
		}
	}
	
	function removeItemFromCat($itemid, $type, $catid)
	{
		$delete = 'DELETE FROM '.DB_PERFIX.'admin_hfb_cats_to_items WHERE type='.$type.' AND cat_id='.$catid.' AND item_id='.$itemid.';';
		$result = mysql_query($delete);
		mysql_query('OPTIMIZE TABLE '.DB_PERFIX.'admin_hfb_cats_to_items;');
		return $result;
	}
	
	function addItemToCat($itemid, $type, $catid)
	{
		$found = 0;
		$select = 'SELECT id FROM '.DB_PERFIX.'admin_hfb_cats_to_items WHERE type='.$type.' AND cat_id='.$catid.' AND item_id='.$itemid.';';
		$result = mysql_query($select);
		while($row = mysql_fetch_array($result))
		{ $found = $row['id']; }
		if($found == 0)
		{
			$insert = 'INSERT INTO '.DB_PERFIX.'admin_hfb_cats_to_items (`type`, `cat_id`, `item_id`)';
			$insert .= 'VALUES ('.$type.', '.$catid.', '.$itemid.');';
			$result = mysql_query($insert);
		}
		return $result;
	}
	
	function getModComSets()
	{
		$i=0;
		$result = mysql_query("SELECT * FROM ".DB_PERFIX."admin_modcom_sets ORDER BY id ASC;");
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['title'] = $row['title'];
			$i++;
		}
		return $item;
	}

	function setTableActive($id, $table, $active)
	{
		$update  = 'UPDATE '.DB_PERFIX.$table.' SET ';
		$update .= 'active='.$active.' ';
		$update .= 'WHERE id='.$id.' LIMIT 1;';
		$update = mysql_query($update);
	}
	
	function setTableItemDefault($id, $table, $active, $defield)
	{
		$update  = 'UPDATE '.DB_PERFIX.$table.' SET ';
		$update .= ''.$defield.'='.$active.' ';
		$update .= 'WHERE id='.$id.' LIMIT 1;';
		$update = mysql_query($update);
	}
	
	function getCssArray($type, $id=0)
	{
		if($type > 0)
		{
			$i=0;
			if($id > 0)
			{ $result = mysql_query('SELECT id, item_id, selector, style FROM '.DB_PERFIX.'admin_css WHERE type='.$type.' AND item_id='.$id.' ORDER BY selector ASC;'); }
			else
			{ $result = mysql_query('SELECT id, item_id, selector, style FROM '.DB_PERFIX.'admin_css WHERE type='.$type.' ORDER BY item_id, selector ASC;'); }
			while($row = mysql_fetch_array($result))
			{
				$item[$i]['id'] = $row['id'];
				$item[$i]['item_id'] = $row['item_id'];
				$item[$i]['selector'] = $row['selector'];
				$item[$i]['style'] = $row['style'];
				$i++;
			}
			return $item;
		}
	}
	
	function reloadHFBTypeCss($type)
	{
		if($type > 0)
		{
			if($type == 1)
			{
				$output = getHeaderCssHTML();
				$store_path = SITE_PATH.'templates/core/headers/headers.css';
			}
			if($type == 2)
			{
				$output = getFooterCssHTML();
				$store_path = SITE_PATH.'templates/core/footers/footers.css';
			}
			if($type == 3)
			{
				$output = getButtonCssHTML();
				$store_path = SITE_PATH.'templates/core/buttons/buttons.css';
			}
			if($type == 6)
			{
				$output = getBorderCssHTML();
				$store_path = SITE_PATH.'templates/core/borders/borders.css';
			}
			if($type == 7)
			{
				$output = getCssArrayHTML($type);
				$store_path = SITE_PATH.'templates/core/menu/menu.css';
			}
			if(file_exists($store_path))
			{ unlink($store_path); }
			file_put_contents($store_path,$output);
		}
	}
	
	function getHeaderCssHTML()
	{
		
		$items = getCssArray(1);
		$html = '';
		if($items)
		{
			foreach($items as $item)
			{
				$html = $html.$item['selector'].' { ';
				$html = $html.$item['style'].' } ';
			}
		}
		return $html;
	}
	
	function getFooterCssHTML()
	{
		
		$items = getCssArray(2);
		$html = '';
		if($items)
		{
			foreach($items as $item)
			{
				$html = $html.$item['selector'].' { ';
				$html = $html.$item['style'].' } ';
			}
		}
		return $html;
	}
	
	function getButtonCssHTML()
	{
		
		$items = getCssArray(3);
		$html = '';
		if($items)
		{
			foreach($items as $item)
			{
				$html = $html.$item['selector'].' { ';
				$html = $html.$item['style'].' } ';
			}
		}
		return $html;
	}
	
	function getBorderCssHTML($id=0)
	{
		if($id > 0)
		{
			$items = getCssArray(6, $id);
		}
		else
		{
			$items = getCssArray(6);
		}
		$html = '';
		if($items)
		{
			foreach($items as $item)
			{
				$html = $html.$item['selector'].' { ';
				$html = $html.$item['style'].' } ';
			}
		}
		return $html;
	}
	
	function getCssArrayHTML($type, $id=0)
	{
		if($type)
		{
			if($id > 0)
			{
				$items = getCssArray($type, $id);
			}
			else
			{
				$items = getCssArray($type);
			}
			$html = '';
			if($items)
			{
				foreach($items as $item)
				{
					$html = $html.$item['selector'].' { ';
					$html = $html.$item['style'].' } ';
				}
			}
			return $html;
		}
	}
	
	function getStyleCss($type, $item_id, $selector)
	{
		$select = 'SELECT style FROM '.DB_PERFIX.'admin_css WHERE type='.$type.' AND item_id='.$item_id.' AND selector="'.$selector.'";';
		$result = mysql_query($select);
		if($result)
		{
			while($row = mysql_fetch_array($result))
			{ $item = $row['style']; }
			return $item;
		}
	}
	
	function updateCss($type, $item_id, $selector, $style)
	{
		$id = 0;
		$select = 'SELECT id, type, selector, style FROM '.DB_PERFIX.'admin_css WHERE type='.$type.' AND item_id='.$item_id.' AND selector="'.$selector.'";';
		$result = mysql_query($select);
		while($row = mysql_fetch_array($result))
		{ $id = $row['id']; }
		if($id > 0)
		{
			if($style == '')
			{ $update = 'DELETE FROM '.DB_PERFIX.'admin_css WHERE id='.$id.';';}
			else
			{ $update = 'UPDATE '.DB_PERFIX.'admin_css SET style="'.$style.'" WHERE id='.$id.';'; }
			$result = mysql_query($update);
		}
		else
		{
			if($style != '')
			{
				$insert = 'INSERT INTO '.DB_PERFIX.'admin_css (`type`, `item_id`, `selector`, `style`)';
				$insert .= 'VALUES ('.$type.', '.$item_id.', "'.$selector.'", "'.$style.'");';
				$result = mysql_query($insert);
			}
		}
	}
	
	function getSelectorsComponent($id=0)
	{
		$i=0;
		if($id > 0)
		{ $result = mysql_query('SELECT id, item_id, selector, style FROM '.DB_PERFIX.'admin_css WHERE type=4 AND item_id='.$id.' ORDER BY selector ASC;'); }
		else
		{ $result = mysql_query("SELECT id, item_id, selector, style FROM ".DB_PERFIX."admin_css WHERE type=4 AND item_id=0 ORDER BY selector ASC;"); }
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['item_id'] = $row['item_id'];
			$item[$i]['selector'] = $row['selector'];
			$item[$i]['style'] = $row['style'];
			$i++;
		}
		return $item;
	}
	
	function getSelectorsModule($id=0)
	{
		$i=0;
		if($id > 0)
		{ $result = mysql_query('SELECT id, item_id, selector, style FROM '.DB_PERFIX.'admin_css WHERE type=5 AND item_id='.$id.' ORDER BY selector ASC;'); }
		else
		{ $result = mysql_query("SELECT id, item_id, selector, style FROM ".DB_PERFIX."admin_css WHERE type=5 AND item_id=0 ORDER BY selector ASC;"); }
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['item_id'] = $row['item_id'];
			$item[$i]['selector'] = $row['selector'];
			$item[$i]['style'] = $row['style'];
			$i++;
		}
		return $item;
	}
	
	function getThemeStyleCss($theme_id, $selector)
	{
		$select = 'SELECT style FROM '.DB_PERFIX.'themes_css WHERE theme_id='.$theme_id.' AND selector="'.$selector.'";';
		$result = mysql_query($select);
		while($row = mysql_fetch_array($result))
		{ $item = $row['style']; }
		return $item;
	}
	
	function getThemeStylesheet($theme_id)
	{
		$select = 'SELECT id, theme_id, selector, style FROM '.DB_PERFIX.'themes_css WHERE theme_id='.$theme_id.';';
		$result = mysql_query($select);
		while($row = mysql_fetch_array($result))
		{ 
			$item[$row['selector']] = $row['style']; 
		}
		return $item;
	}
	
	function getThemeStyleList($theme_id, $type=0)
	{
		if($type < 1)
		{ $type=0; }
		
		$select = 'SELECT id, theme_id, selector, style, bg_image FROM '.DB_PERFIX.'themes_css WHERE theme_id='.$theme_id.' AND type='.$type.' ORDER BY selector ASC;';
		$result = mysql_query($select);
		$i=0;
		while($row = mysql_fetch_array($result))
		{ 
			$item[$i]['id'] = $row['id']; 
			$item[$i]['type'] = $row['type'];
			$item[$i]['selector'] = $row['selector']; 
			$item[$i]['style'] = $row['style'];
			$item[$i]['bg_image'] = $row['bg_image'];
			$i++;
		}
		return $item;
	}
	
	function getDefaultSelectors()
	{
		$select = 'SELECT id, selector FROM '.DB_PERFIX.'admin_css_selectors ORDER BY selector_order ASC, id ASC;';
		$result = mysql_query($select);
		$i=0;
		while($row = mysql_fetch_array($result))
		{ 
			$item[$i]['id'] = $row['id']; 
			$item[$i]['selector'] = $row['selector']; 
			$i++;
		}
		return $item;
	}
	
	function getThemeSelectors($type=0)
	{
		$select = 'SELECT id, selector FROM '.DB_PERFIX.'admin_css_theme_selectors WHERE type='.$type.' OR type=0 ORDER BY type ASC, selector ASC;';
		$result = mysql_query($select);
		$i=0;
		while($row = mysql_fetch_array($result))
		{ 
			$item[$i]['id'] = $row['id']; 
			$item[$i]['selector'] = $row['selector']; 
			$i++;
		}
		return $item;
	}
	
	function getBorderAdmin($id)
	{
		$result = mysql_query('SELECT * FROM '.DB_PERFIX.'com_borders WHERE id= '.$id.';');
		while($row = mysql_fetch_array($result))
		{
			$border['id'] = $row['id'];
			$border['type'] = $row['type'];
			$border['name'] = $row['name'];
			$border['width'] = $row['width'];
			$border['image_bg'] = $row['image_bg'];
			$border['image_top'] = $row['image_top'];
			$border['image_top_l'] = $row['image_top_l'];
			$border['image_top_r'] = $row['image_top_r'];
			$border['image_bot'] = $row['image_bot'];
			$border['image_bot_l'] = $row['image_bot_l'];
			$border['image_bot_r'] = $row['image_bot_r'];
			$border['image_mid_l'] = $row['image_mid_l'];
			$border['image_mid_r'] = $row['image_mid_r'];
			$border['active'] = $row['active'];
			$border['css'] = getCssArray(6, $row['id']);
		}
		return $border;
	}
	
	function replaceFieldStringInTableById($table, $field, $search, $replace)
	{
		$output = '';
		if(($table != '') && ($field != '') && ($search != '') && ($replace != ''))
		{
			$sql = 'SELECT id, '.$field.' FROM '.DB_PERFIX.$table.';';
			$result = mysql_query($sql);
			while($row = mysql_fetch_array($result))
			{
				$id = $row['id'];
				$fieldValue = $row[$field];
				$newValue	= str_replace($search,$replace,$fieldValue);
				if(($fieldValue != $newValue) && ($id > 0))
				{
					$update = 'UPDATE '.DB_PERFIX.$table.' SET '.$field.'="'.$newValue.'" WHERE id='.$id.';';
					$output .= $update.'<br />';
					$result_update = mysql_query($update);
				}
			}
		}
		return $output;
	}
	
	function stripNonAlpha($str)
	{
		$str = preg_replace("/[^a-zA-Z\s]/", "", $str);
		return strip_tags(trim($str));
	}
	
	function stripNonAlphaNum($str)
	{
		$str = preg_replace("/[^a-zA-Z0-9\s]/", "", $str);
		return strip_tags(trim($str));
	}
	
	function stripNum($val)
	{
		$out = eregi_replace("([0-9]+)","",$val);
		return ($out);
	}
	
	function stripAlpha($val)
	{
		$out = eregi_replace("([A-Z]+)","",$val);
		return $out;
	}
	
	function stripSpace($val)
	{
		str_replace(' ','',$val);
		return $val;
	}
	
	function hasSpace($val)
	{
		if( ereg("[ 	]",$val) )
		{ return true; }
		else
		{ return false;	}
	}
	
	function isAllNum($val)
	{
		if((gettype($val)) == "integer")	
		{ return true; }
		return false;
	}
	
	function isEmail($email)
	{
	   $isValid = true;
	   $atIndex = strrpos($email, "@");
	   if (is_bool($atIndex) && !$atIndex)
	   { $isValid = false; }
	   else
	   {
		  $domain = substr($email, $atIndex+1);
		  $local = substr($email, 0, $atIndex);
		  $localLen = strlen($local);
		  $domainLen = strlen($domain);
		  if ($localLen < 1 || $localLen > 64)
		  { $isValid = false; }
		  else if ($domainLen < 1 || $domainLen > 255)
		  { $isValid = false; }
		  else if ($local[0] == '.' || $local[$localLen-1] == '.')
		  { $isValid = false; }
		  else if (preg_match('/\\.\\./', $local))
		  { $isValid = false; }
		  else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
		  { $isValid = false;  }
		  else if (preg_match('/\\.\\./', $domain))
		  { $isValid = false; }
		  else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
		  {
			 // character not valid in local part unless 
			 // local part is quoted
			 if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local)))
			 { $isValid = false; }
		  }
		  /*
		  if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
		  {
			 // domain not found in DNS
			 $isValid = false;
		  }
		  */
	   }
	   return $isValid;
	}


	function escape($string) 
	{
		if(get_magic_quotes_gpc()) $string = stripslashes($string);
		return mysql_real_escape_string($string);
	}
	
	function dbVal($val, $type)
	{
		switch ($type)
		{
			case 'int':
				if(is_numeric($val))
				{ return $val; }
				else
				{ return 0; }
			case 'txt':
				return '"'.$val.'"';
		}
	}
	
?>